<template>
	<view class="login">
		<image src="../../static/logo_login.png" mode="" style="width: 258upx;height: 258upx;"></image>
		<image src="../../static/text_login.png" mode="" style="width: 478upx;height: 186upx;margin-top: 174upx;"></image>
		<button open-type="getUserInfo" @getuserinfo="getUserInfo" class="btn-login">授权登录 <uni-icon type="arrowright" size="20" color="#45AA8C"></uni-icon></button>
	</view>
</template>

<script>
	import uniIcon from '../../components/uni-icon.vue'
	export default {
		components: {
			uniIcon
		},
		data() {
			var isLogin = uni.getStorageSync("token") != null && uni.getStorageSync("token") != "";
			return {
				isLogin: isLogin,
				from:"",
			};
		},
		onLoad:function(e) {
			if(e.from){
				this.from=decodeURIComponent(e.from);
			}else{
				this.from="";
			}
			if(this.isLogin){
				var that=this;
				uni.showLoading({
					title:"加载中",
				});
				//检查用户登录状态
				that.ajax("/login/check",{
					token:uni.getStorageSync("token"),
				},function(res){
					if(res.data.code==1){
						uni.reLaunch({
							url:"../index/index",
						})
					}else if(res.data.code==2){
						
						uni.reLaunch({
							url:"../guide/guide?avatar="+res.data.data.avatar+"&nickname="+res.data.data.nickname,
						})
					}else{
						that.isLogin=false;
					}
				});
			}
			
		},
		methods:{
			getUserInfo:function(e){
				uni.showLoading({
					title:"登录中",
				})
				var that=this;
				uni.login({
					provider:"wexin",
					success:function(res){
						uni.getUserInfo({
							provider:"wexin",
							success:function(info){
								that.ajax("/login/login/",{
									code:res.code,
									avatar:info.userInfo.avatarUrl,
									nickname:info.userInfo.nickName,
									sex:info.userInfo.gender,
									token:info.signature,
								},function(res){
									uni.setStorageSync("token",info.signature);
									if(res.data.code==1){
										if(that.from){
											uni.navigateBack({})
										}else{
											uni.reLaunch({
												url:"/pages/index/index",
											})
										}
									}else if(res.data.code==2){
										uni.redirectTo({
											url: "../guide/guide?avatar="+info.userInfo.avatarUrl+"&nickname="+info.userInfo.nickName+"&from="+encodeURIComponent(that.from),
										});
									}
								});
							}
						})
					}
				})
			},
			
		}
	}
</script>

<style>
	page{
		height: 100%;
		background: #478E7E!important;
		display: flex;
		align-items: center;
		align-content: center;
		width: 100%;
	}
	.login{
		display: flex;
		flex-direction: column;
		align-items: center;
		align-content: center;
		width: 100%;
	}
	.btn-login{
		margin-top: 256upx;
		padding: 18upx 48upx;
		height:81upx;
		background:#ffffff;
		box-shadow:0upx 10upx 20upx 0upx rgba(20,105,86,0.45);
		border-radius:41upx;
		line-height: 1.4;
		font-size: 35upx;
		color: #478E7E;
	}
</style>
